from judge.crer.cr import CRFactory
from problem.models import ProblemTail

# 拼接代码
def splice_code(problem_id, language, header):
        tails = ProblemTail.objects.get(problem_id = problem_id)
        if language == "C++":
              tail = tails.cpp_tail
        elif language == "Java":
                tail = tails.java_tail
        elif language == "Python3":
                tail = tails.python3_tail
        else:
                tail = ""
                 
        code = header + "\n" + tail
        return code     

# 判题
def judge(problem_id, language, code, cpu_limit, mem_limit):
        # 拼接代码
        spliced_code = splice_code(problem_id,language,code)
        # 获取cr对象
        cr = CRFactory.get_compile_and_run(language)
        out_obj = cr.start(spliced_code,int(cpu_limit),int(mem_limit))
        return out_obj